home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Samples / SampleCode / MetafileRead / MetaFileReadShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-13  |  8.2 KB  |  336 lines  |  [TEXT/MPCC]

  1. // Quickdraw 3D sample code
  2. //
  3. // Nick Thompson, AppleLink: DEVSUPPORT (devsupport@applelink.apple.com)
  4. //
  5. // ©1994-5 Apple Computer Inc., All Rights Reserved
  6.  
  7. // system headers
  8. #include <Desk.h>
  9. #include <Dialogs.h>
  10. #include <DiskInit.h>
  11. #include <Fonts.h>
  12. #include <Menus.h>
  13. #include <PictUtil.h>
  14. #include <QDOffScreen.h>
  15. #include <QuickDraw.h>
  16. #include <SegLoad.h>
  17. #include <StandardFile.h>
  18. #include <TextEdit.h>
  19. #include <ToolUtils.h>
  20.  
  21. // for QuickDraw 3D
  22. #include "QD3D.h"
  23. #include "QD3DMath.h"
  24. #include "QD3DDrawContext.h"
  25. #include "QD3DShader.h"
  26. #include "QD3DTransform.h"
  27. #include "QD3DGroup.h"
  28. #include "QD3DCamera.h"
  29.  
  30. #include "MetaFileReadShell.h"
  31. #include "MetaFileReadSupport.h"
  32. #include "MyErrorHandler.h"
  33.  
  34. //-------------------------------------------------------------------------------------------
  35. // function prototypes
  36.  
  37. static void         InitToolbox( void ) ;
  38. static void         MainEventLoop( void ) ;
  39. static void         HandleKeyPress(EventRecord *event) ;
  40. static void         HandleOSEvent(EventRecord *event) ;
  41. void InitDocumentData( DocumentPtr theDocument ) ;
  42. TQ3Status DocumentDraw3DData( DocumentPtr theDocument ) ;
  43. void DisposeDocumentData( DocumentPtr theDocument) ;
  44.  
  45. //-------------------------------------------------------------------------------------------
  46. //
  47.  
  48. Boolean         gQuitFlag         = false ;
  49. WindowPtr        gMainWindow        = nil ;
  50. DocumentRec        gDocument ;
  51.  
  52. //-------------------------------------------------------------------------------------------
  53. // main()
  54. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  55. // and enter the main event loop.  On exit from the main event loop, we want to call
  56. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  57.  
  58. void main(void)
  59. {
  60.     TQ3Status    myStatus;
  61.     Rect        rBounds = { 50, 50, 350, 350 } ;
  62.     Str255        title = "\pSpinning Box" ;
  63.     FSSpec        theFileSpec ;                // the file we are opening
  64.  
  65.     InitToolbox() ;
  66.  
  67.     if(MetafileFileSpecify( &theFileSpec )) {
  68.     
  69.         SetCursor(*(GetCursor(watchCursor)));
  70.         
  71.         //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  72.         myStatus = Q3Initialize();
  73.         if ( myStatus == kQ3Failure ) {
  74.             DebugStr("\pErInitialize returned failure.");    
  75.             ExitToShell() ;
  76.         }
  77.                         
  78.         // install the error handler - this gets called whenever
  79.         // an error occurs, which means we don't have to check so 
  80.         // much
  81.         Q3Error_Register( MyErrorHandler, 0L );        
  82.         Q3Warning_Register( MyWarningHandler, 0L );        
  83.     
  84.         // set up our globals
  85.         gQuitFlag = false ;
  86.         gMainWindow = NewCWindow(nil,&rBounds,title,false,noGrowDocProc,(WindowPtr)-1,true,0) ;
  87.  
  88.         // initialise our document structure
  89.         InitDocumentData( &gDocument ) ;
  90.         
  91.         // try to read the file into the main display group
  92.         if((gDocument.fModel = MyNewModelFromFile(&theFileSpec)) != NULL ) {        
  93.  
  94.             AdjustCamera(    &gDocument,
  95.                             (gMainWindow->portRect.right - gMainWindow->portRect.left),
  96.                             (gMainWindow->portRect.bottom - gMainWindow->portRect.top) ) ;
  97.  
  98.             SetWTitle( gMainWindow, theFileSpec.name );
  99.             ShowWindow( gMainWindow ) ;
  100.             SetPort( gMainWindow ) ;
  101.     
  102.             SetCursor(&qd.arrow) ;
  103.             MainEventLoop();
  104.             
  105.         }
  106.                 
  107.         DisposeDocumentData( &gDocument ) ;
  108.         
  109.         //    Close our connection to the QuickDraw 3D library
  110.         myStatus = Q3Exit();
  111.         if ( myStatus == kQ3Failure )
  112.             DebugStr("\pErExit returned failure.");
  113.     }    
  114. }
  115.  
  116. //-------------------------------------------------------------------------------------------
  117. //
  118.  
  119. void InitDocumentData( DocumentPtr theDocument ) 
  120. {
  121.     TQ3Point3D        myOrigin = { 0, 0, 0 } ;
  122.     
  123.     // sets up the 3d data for the scene
  124.     //    Create view for QuickDraw 3D.
  125.     theDocument->fView = MyNewView( gMainWindow );
  126.  
  127.     // the main display group:
  128.     theDocument->fModel = NULL ;
  129.     
  130.     
  131.     // the drawing styles:
  132.     theDocument->fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleNone) ;
  133.     theDocument->fBackFacing = Q3BackfacingStyle_New(kQ3BackfacingStyleRemove ) ;
  134.     theDocument->fFillStyle = Q3FillStyle_New(kQ3FillStyleFilled ) ;
  135.  
  136.  
  137.     theDocument->fGroupScale = 1;                
  138.     theDocument->fGroupCenter = myOrigin ;            
  139.  
  140.     // set the rotation matrix the identity matrix
  141.     Q3Matrix4x4_SetIdentity(&theDocument->fRotation);    
  142.                     
  143.     return ;
  144.     
  145. bail:
  146.     // either we failed getting the model or the view
  147.     // so we want to quit here
  148.     ExitToShell() ;
  149.     
  150. }
  151.  
  152. void DisposeDocumentData( DocumentPtr theDocument)
  153. {
  154.     Q3Object_Dispose(theDocument->fView) ;                // the view for the scene
  155.     Q3Object_Dispose(theDocument->fModel) ;                // object in the scene being modelled
  156.     Q3Object_Dispose(theDocument->fInterpolation) ;        // interpolation style used when rendering
  157.     Q3Object_Dispose(theDocument->fBackFacing) ;        // whether to draw shapes that face away from the camera
  158.     Q3Object_Dispose(theDocument->fFillStyle) ;            // whether drawn as solid filled object or decomposed to components
  159. }
  160. //-----------------------------------------------------------------------------
  161. // assumes the port is set up before being called
  162.  
  163. TQ3Status DocumentDraw3DData( DocumentPtr theDocument )
  164. {
  165.     TQ3Vector3D                globalScale;
  166.     TQ3Vector3D                globalTranslate;
  167. //{
  168. //    TQ3CameraData        theCameraData ;
  169. //    TQ3CameraObject        theCameraObject;
  170. //    
  171. //    Q3View_GetCamera(theDocument->fView, &theCameraObject);
  172. //    Q3Camera_GetData(theCameraObject, &theCameraData);
  173. //Debugger() ;
  174. //
  175. //}
  176.     TQ3Status theStatus ;    
  177.     
  178.     Q3View_StartRendering(theDocument->fView) ;
  179.     do {
  180.         theStatus = SubmitScene( theDocument ) ;
  181.     } while (Q3View_EndRendering(theDocument->fView) == kQ3ViewStatusRetraverse );
  182.  
  183.     return theStatus ;    
  184.  
  185.     
  186. }
  187.  
  188.  
  189. //----------------------------------------------------------------------------------
  190.  
  191. //-------------------------------------------------------------------------------------------
  192. //
  193.  
  194. short HiWrd(long aLong)
  195. {
  196.     return    (((aLong) >> 16) & 0xFFFF) ;
  197. }
  198.  
  199. //-------------------------------------------------------------------------------------------
  200. //
  201.  
  202. short LoWrd(long aLong)
  203. {
  204.     return    ((aLong) & 0xFFFF) ;
  205.  
  206. }
  207.  
  208. //-------------------------------------------------------------------------------------------
  209. //
  210.  
  211. void InitToolbox()
  212. {
  213.     Handle        menuBar = nil;
  214.  
  215.     MaxApplZone() ;
  216.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  217.     
  218.     InitGraf( &qd.thePort );
  219.     InitFonts();
  220.     InitWindows();
  221.  
  222.     FlushEvents( everyEvent, 0 ) ;
  223.     // initialize application globals
  224.     
  225.     gQuitFlag = false;
  226.     InitCursor();
  227.     
  228. }
  229.  
  230.  
  231. //-------------------------------------------------------------------------------------------
  232. //
  233. void MainEventLoop()
  234. {
  235.     EventRecord     event;
  236.     WindowPtr       window;
  237.     short           thePart;
  238.     Rect            screenRect, updateRect;
  239.     Point            aPoint = {100, 100};
  240.     CGrafPtr        savedPort ;
  241.     
  242.  
  243.     while( !gQuitFlag )
  244.     {
  245.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  246.         {
  247.  
  248.             switch (event.what) {
  249.                 case mouseDown:
  250.                 
  251.                     thePart = FindWindow( event.where, &window );
  252.                     
  253.                     switch( thePart ) {
  254.                         case inMenuBar: 
  255.                             break;
  256.                         
  257.                         case inDrag:
  258.                     
  259.                             screenRect = (**GetGrayRgn()).rgnBBox;
  260.                             DragWindow( window, event.where, &screenRect );
  261.                             break ;
  262.                     
  263.                         case inContent:
  264.                     
  265.                             if (window != FrontWindow())
  266.                                 SelectWindow( window );
  267.                             break ;
  268.                     
  269.                         case inGoAway:
  270.                             if (TrackGoAway( window, event.where )) {
  271.                                 DisposeWindow ( window );
  272.                                 gQuitFlag = true;
  273.  
  274.                             }
  275.                             break ;
  276.                             
  277.                         default:
  278.                             break ;
  279.                     }
  280.                     break ;
  281.                             
  282.                         
  283.                 case updateEvt:
  284.                 
  285.                     window = (WindowPtr)event.message;
  286.                     updateRect = (**(window->visRgn)).rgnBBox;
  287.                     SetPort( window ) ;
  288.                     BeginUpdate( window );
  289.                     DocumentDraw3DData( &gDocument ) ;
  290.                     EndUpdate( window );
  291.  
  292.                     break ;
  293.                     
  294.                 case keyDown:
  295.                 case autoKey:
  296.                     HandleKeyPress(&event);
  297.                     break;
  298.                     
  299.                 case diskEvt:
  300.                     if ( HiWrd(event.message) != noErr ) 
  301.                         (void) DIBadMount(aPoint, event.message);
  302.                     break;
  303.                     
  304.                 case osEvt:
  305.                 case activateEvt:
  306.                     break;
  307.  
  308.  
  309.             }
  310.         }
  311.         else {
  312.             // we received a null event, rotate the cube
  313.             TQ3Matrix4x4    tmp;
  314.             Rect        theRect = ((GrafPtr)gMainWindow)->portRect ;
  315.             
  316.             SetPort((GrafPtr)gMainWindow) ;
  317.             Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.1, 0.12, 0.08);
  318.             Q3Matrix4x4_Multiply(&gDocument.fRotation, &tmp, &gDocument.fRotation);
  319.  
  320.             InvalRect( &theRect ) ;
  321.         }
  322.     }
  323. }
  324.  
  325.  
  326. //-------------------------------------------------------------------------------------------
  327. //
  328. void HandleKeyPress(EventRecord *event)
  329. {}
  330.  
  331. //-------------------------------------------------------------------------------------------
  332. //
  333.  
  334.  
  335.  
  336.